package client.cell;

import client.dialog.ConfirmDialog;
import common.bodies.GroupShipBody;
import common.bodies.UserBody;
import javafx.scene.control.*;


public class GroupCell extends ListCell<GroupShipBody> {
    private final UserBody currentUser;

    private OnClickGroupHandler onClickGroup;
    private OnInviteFriendHandler onInviteFriend;
    private OnQuitGroupHandler onQuitGroup;
    private OnDismissGroupHandler onDismissGroup;
    private OnKickMemberHandler onKickMember;
    private OnMuteManageHandler onMuteManage;
    public GroupCell(UserBody currentUser) {
        this.currentUser = currentUser;
        setStyle("-fx-font-size: 14px; -fx-padding: 5px;");
    }

    @Override
    protected void updateItem(GroupShipBody item, boolean empty) {
        super.updateItem(item, empty);

        if (empty || item == null) {
            setText(null);
            setContextMenu(null);
        } else {
            setText(item.getGroupName());

            ContextMenu contextMenu = new ContextMenu();

            MenuItem allGroupMembers =new MenuItem(" 群友列表");
            allGroupMembers.setOnAction(e-> {



            });


            MenuItem inviteItem = new MenuItem("➕ 拉好友入群");
            inviteItem.setOnAction(e -> {
                if (onInviteFriend != null) onInviteFriend.handle(item);
            });

            MenuItem kickItem = new MenuItem("👞 踢人出群");
            kickItem.setOnAction(e -> {
                if ( onKickMember != null) {
                    onKickMember.handle(item);
                }

            });

            MenuItem dismissItem = new MenuItem("💣 解散群聊");
            dismissItem.setOnAction(e -> {
                boolean confirmed = ConfirmDialog.show("确认操作", "你确定要解散群聊吗？该操作不可恢复！");
                if (confirmed && onDismissGroup != null) {
                    onDismissGroup.handle(item);
                }
            });

            MenuItem quitItem = new MenuItem("🚪 退出群聊");
            quitItem.setOnAction(e -> {
                boolean confirmed = ConfirmDialog.show("确认操作", "你确定要退出这个群聊吗？");
                if (confirmed && onQuitGroup != null) {
                    onQuitGroup.handle(item);
                }
            });


            MenuItem muteItem = new MenuItem("🔇 禁言管理");
            muteItem.setOnAction(e -> {
                if (onMuteManage != null) {
                    onMuteManage.handle(item);
                }
            });


            if (item.getRole()==2) {
                contextMenu.getItems().addAll(inviteItem, dismissItem, kickItem,muteItem);
            } else {
                contextMenu.getItems().addAll(inviteItem, quitItem);
            }

            setContextMenu(contextMenu);

            setOnMouseClicked(e -> {
                if (e.getClickCount() == 1 && onClickGroup != null) {
                    onClickGroup.handle(item);
                }
            });
        }
    }

    // 接口设置方法
    public void setOnClickGroup(OnClickGroupHandler handler) {
        this.onClickGroup = handler;
    }

    public void setOnInviteFriend(OnInviteFriendHandler handler) {
        this.onInviteFriend = handler;
    }

    public void setOnQuitGroup(OnQuitGroupHandler handler) {
        this.onQuitGroup = handler;
    }

    public void setOnDismissGroup(OnDismissGroupHandler handler) {
        this.onDismissGroup = handler;
    }

    public void setOnKickMember(OnKickMemberHandler handler) {
        this.onKickMember = handler;
    }

    public void setOnMuteManage(OnMuteManageHandler handler) {
        this.onMuteManage = handler;
    }


    // 接口定义
    public interface OnClickGroupHandler {
        void handle(GroupShipBody group);
    }

    public interface OnInviteFriendHandler {
        void handle(GroupShipBody group);
    }

    public interface OnQuitGroupHandler {
        void handle(GroupShipBody group);
    }

    public interface OnDismissGroupHandler {
        void handle(GroupShipBody group);
    }

    public interface OnKickMemberHandler {
        void handle(GroupShipBody group);
    }

    public interface OnMuteManageHandler {
        void handle(GroupShipBody group);
    }

}
